package com.centerm.bluetooth.common.controller;

import android.content.Context;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.centerm.bluetooth.common.Command;
import com.centerm.bluetooth.common.listener.IScreenListener;
import com.centerm.bluetooth.core.controller.BaseController;
import com.centerm.bluetooth.core.controller.constants.WaitEnum;
import com.centerm.bluetooth.ibridge.BluetoothIBridgeDevice;
import com.centerm.bluetooth.model.Respond;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by linwenhui on 2017/5/2.
 */

public class ScreenController extends BaseController<IScreenListener> {
    private final static int TYPE_TERMINAL_DISPLAY = 0x01;
    private int type;

    public ScreenController(@NonNull Context context) {
        super((byte) 0x07,context);
    }

    public boolean terminalDisplay(@IntRange(from = 0, to = 0x14) int line2Offset, String line2,
                                   @IntRange(from = 0, to = 0x14) int line3Offset, String line3,
                                   @IntRange(from = 0, to = 0x14) int line4Offset, String line4, @IntRange(from = 0) int timeout) throws IOException, UnsupportedEncodingException {
        if (wait == WaitEnum.FREE) {
            ByteArrayOutputStream params = new ByteArrayOutputStream();
            if (TextUtils.isEmpty(line2)) {
                params.write(0x00);
                params.write(0x00);
            } else {
                byte[] line2Bytes = line2.getBytes("GBK");
                params.write(line2Offset);
                params.write(line2Bytes.length);
                params.write(line2Bytes);
            }

            if (TextUtils.isEmpty(line3)) {
                params.write(0x00);
                params.write(0x00);
            } else {
                byte[] line3Bytes = line3.getBytes("GBK");
                params.write(line3Offset);
                params.write(line3Bytes.length);
                params.write(line3Bytes);
            }

            if (TextUtils.isEmpty(line4)) {
                params.write(0x00);
                params.write(0x00);
            } else {
                byte[] line4Bytes = line4.getBytes("GBK");
                params.write(line4Offset);
                params.write(line4Bytes.length);
                params.write(line4Bytes);
            }
            params.write(timeout);
            type = TYPE_TERMINAL_DISPLAY;
            boolean res = send(Command.TERMINAL_DISPLAY, params.toByteArray());
            if (res)
                setTimeoutTimer();
            params.close();
            return res;
        }
        return false;
    }

    @Override
    public void onDataReceived(BluetoothIBridgeDevice device, Respond respond) {
        super.onDataReceived(device, respond);
        if (type == TYPE_TERMINAL_DISPLAY) {
            parseTerminalDisplay();
        }
    }

    protected void parseTerminalDisplay() {
        if (baseListener != null)
            baseListener.onScreenDisplay();
    }
}
